﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

namespace Ue.CQRS.Samples.Infrastructure.MessageManagent
{
    public class MessageRender : IMessage
    {
        private ClientScriptManager scriptManager;

        public MessageRender() 
            : this(null) { }

        public MessageRender(Page page)
        {
            this.scriptManager = page == null
                ? (HttpContext.Current.Handler as Page).ClientScript
                : page.ClientScript;
        }

        public void Alert(string message)
        {
            if (string.IsNullOrWhiteSpace(message))
            {
                return;
            }

            var script = string.Format("alert('{0}');", message);

            this.scriptManager.RegisterClientScriptBlock(typeof(Page), "alertMessage", script, true);
        }
    }
}